/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：充值中心出入金接口实现                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-02-20 15:07:13                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Repository.SqlServer                                  
*│　类    名： UCInOutCashRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using BestTrader.Cms.Core.DbHelper;
using BestTrader.Cms.Core.Options;
using BestTrader.Cms.Core.Repository;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;

namespace BestTrader.Cms.Repository.SqlServer
{
    public class UCInOutCashRepository:BaseRepository<UCInOutCash,Int32>, IUCInOutCashRepository
    {
        public UCInOutCashRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("BT.TEST");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public decimal SumMoneyByUserId(string UserId)
        {
            // 所有完成了的和CashType=1 出金未完成的记录
            string sql = "select ISNULL(SUM(Money), 0) from [UCInOutCash] where IsDelete = 0 and (State=1 or (State=0 and CashType = 1)) and UserManagerId=@Id";
            return _dbConnection.QueryFirstOrDefault<decimal>(sql, new
            {
                Id = UserId,
            });
        }

        public decimal SumRMBMoneyByUserId(string UserId)
        {
            string sql = "select ISNULL(SUM(RMBMoney), 0) from [UCInOutCash] where IsDelete = 0 and (State=1 or (State=0 and CashType = 1) and UserManagerId=@Id";
            return _dbConnection.QueryFirstOrDefault<decimal>(sql, new
            {
                Id = UserId,
            });
        }

        public int DeleteLogical(int[] ids)
        {
            string sql = "update [UCInOutCash] set IsDelete=1 where Id in @Ids";
            return _dbConnection.Execute(sql, new
            {
                Ids = ids
            });
        }

        public async Task<int> DeleteLogicalAsync(int[] ids)
        {
            string sql = "update [UCInOutCash] set IsDelete=1 where Id in @Ids";
            return await _dbConnection.ExecuteAsync(sql, new
            {
                Ids = ids
            });
        }
    }
}